package com.vn.gofriends.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.vn.gofriends.R;
import com.vn.gofriends.adapter.MyTripsAdapter;
import com.vn.gofriends.item.City;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class DestinationActivity  extends Activity{

	ListView lvDestination;
	List<City> cities ;
	// Declare Variables
	List<ParseObject> ob;
	ProgressDialog mProgressDialog;
	MyTripsAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.destination_layout);
		new RemoteDataTask().execute();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_destination, menu);
		return super.onCreateOptionsMenu(menu);
	}
	// RemoteDataTask AsyncTask
		private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				
				// Create a progressdialog
				mProgressDialog = new ProgressDialog(DestinationActivity.this);
				// Set progressdialog title
				mProgressDialog.setTitle("Load city");
				// Set progressdialog message
				mProgressDialog.setMessage("Loading...");
				mProgressDialog.setIndeterminate(false);
				// Show progressdialog
				mProgressDialog.show();
			}

			@Override
			protected Void doInBackground(Void... params) {
				// Locate the class table named "Country" in Parse.com
				ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
						"City");
				query.orderByDescending("createdAt");
				try {
					ob = query.find();
				} catch (ParseException e) {
					Log.e("Error", e.getMessage());
					e.printStackTrace();
				}
				return null;
			}

			@Override
		protected void onPostExecute(Void result) {
			// Locate the listview in listview_main.xml
			lvDestination = (ListView) findViewById(R.id.lvDestination);
			cities = new ArrayList<City>();
			// Pass the results into an ArrayAdapter

			// Retrieve object "name" from Parse.com database
			for (ParseObject country : ob) {
				City c = new City();
				c.setIdCity(country.getObjectId());
				c.setCityName(country.getString("cityName"));
				cities.add(c);
			}
			// Binds the Adapter to the ListView
			adapter = new MyTripsAdapter(DestinationActivity.this,
					R.layout.item_listview_place, cities);
			lvDestination.setAdapter(adapter);
			//even toast
			lvDestination.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					Intent intent = new Intent(DestinationActivity.this,PlaceActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("destination", cities.get(arg2));
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.putExtra("extraDestination", bundle);
					startActivity(intent);
				}
			});
			// Close the progressdialog
			mProgressDialog.dismiss();
			// Capture button clicks on ListView items
		}
		}
}
